package com.zjut.an.adplace.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjut.an.config.dao.AnConfig;

/**
 * A data access object (DAO) providing persistence and search support for
 * AnAdplace entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zjut.an.adplace.dao.AnAdplace
 * @author MyEclipse Persistence Tools
 */

public class AnAdplaceDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(AnAdplaceDAO.class);
	// property constants
	public static final String AP_NAME = "apName";
	public static final String AP_NO = "apNo";
	public static final String AP_WIDTH = "apWidth";
	public static final String AP_HEIGHT = "apHeight";
	public static final String AP_ACTIVE = "apActive";

	protected void initDao() {
		// do nothing
	}

	public void sort(Criteria criteria, String sort, String order) {
		if (sort!=null && order!=null) {
			if (order.equals("asc")) {
				criteria.addOrder(Order.asc(sort));
			} else {
				criteria.addOrder(Order.desc(sort));
			}
		} else {
			criteria.addOrder(Order.desc(AP_NO));			
		}
	}
	
	public void save(AnAdplace transientInstance) {
		log.debug("saving AnAdplace instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(AnAdplace transientInstance) {
		log.debug("updating AnAdplace instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void delete(AnAdplace persistentInstance) {
		log.debug("deleting AnAdplace instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public AnAdplace findById(java.lang.Integer id) {
		log.debug("getting AnAdplace instance with id: " + id);
		try {
			AnAdplace instance = (AnAdplace) getHibernateTemplate().get(
					"com.zjut.an.adplace.dao.AnAdplace", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(AnAdplace instance) {
		log.debug("finding AnAdplace instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding AnAdplace instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from AnAdplace as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByApName(Object apName) {
		return findByProperty(AP_NAME, apName);
	}

	public List findByApNo(Object apNo) {
		return findByProperty(AP_NO, apNo);
	}

	public List findByApWidth(Object apWidth) {
		return findByProperty(AP_WIDTH, apWidth);
	}

	public List findByApHeight(Object apHeight) {
		return findByProperty(AP_HEIGHT, apHeight);
	}

	public List findByApActive(Object apActive) {
		return findByProperty(AP_ACTIVE, apActive);
	}

	public List findAll() {
		log.debug("finding all AnAdplace instances");
		try {
			String queryString = "from AnAdplace";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public AnAdplace merge(AnAdplace detachedInstance) {
		log.debug("merging AnAdplace instance");
		try {
			AnAdplace result = (AnAdplace) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(AnAdplace instance) {
		log.debug("attaching dirty AnAdplace instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AnAdplace instance) {
		log.debug("attaching clean AnAdplace instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AnAdplaceDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AnAdplaceDAO) ctx.getBean("AnAdplaceDAO");
	}
}